import {
  mymedal,
  getUserInfo,
  baseUrl
} from '../../api/index.js'

const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    modalHidden: true,
    userInfo: {},
    day: 0,
    login: false,
    show: false,
    imgUrl: ''
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      login: getApp().globalData.login
    })
    console.log(getApp().globalData.login)
    this.getMymedal()
  },

  //分享
  onShareAppMessage: function () {
    return {
      title: '我正在使用SMART睡眠训练计划，超有用！推荐给你',
      imageUrl: '../../images/98875.png'

    }
  },

  getMymedal() {
    mymedal().then(res => {
      if (res.errcode == '00000') {
        let arr = res.medals
        console.log(arr)
        if (arr.length > 0) {
          let day = arr[arr.length - 1].day
          if (day == 7) {
            this.setData({
              imgUrl: '../../images/icon_7.png'
            })
          }
          if (day == 14) {
            this.setData({
              imgUrl: '../../images/icon_14.png'
            })
          }
          if (day == 21) {
            this.setData({
              imgUrl: '../../images/icon_21.png'
            })
          }
          if (day == 28) {
            this.setData({
              imgUrl: '../../images/icon_28.png'
            })
          }
          this.setData({ day })
        }
      }
    })

  },

  //我的睡眠
  handleMySleep() {
    wx.navigateTo({
      url: '/pages/my-sleep/my-sleep'
    })
  },

  //我的徽章
  handleMyInsignia: function (e) {
    wx.navigateTo({
      url: '/pages/my-medal/my-medal'
    })
  },

  //我的计划
  handleMyPlan: function () {
    wx.navigateTo({
      url: '../my-plan/my-plan'
    })
  },

  //意见反馈
  handleFeedback() {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    })
  },

  //联系我们
  handleShowContact() {
    this.setData({
      show: true
    })
  },

  //关闭联系我们
  handleHideContact() {
    this.setData({
      show: false
    })
  },

  //登录
  handleLogin(e) {
    getUserInfo(this);
  },

  //长按显式系统信息
  handleLongPress(e) {
    console.log('handleLongPress')
    console.log(wx.getAccountInfoSync())
    const accountInfo = wx.getAccountInfoSync();
    let appType = ''
    if (accountInfo.miniProgram.envVersion == 'develop') {
      appType = '开发版小程序'
    } else if (accountInfo.miniProgram.envVersion == 'trial') {
      appType = '体验版小程序'
    } else {
      appType = '正式版小程序 v' + accountInfo.miniProgram.version
    }
    let backendType = ''
    if (baseUrl.indexOf('dev.ezhimian.com') >= 0) {
      backendType = '测试版后台'
    } else {
      backendType = '正式版后台'
    }
    wx.showModal({
      title: '提示',
      content: appType + '\r\n' + backendType,
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})
